package com.adtech.adms.order.capitek;

import java.sql.Timestamp;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
import org.apache.log4j.Logger;

import com.adtech.adms.device.pojo.GuiDevice;
import com.adtech.adms.device.pojo.GuiOrder;
import com.adtech.adms.device.service.DeviceService;
import com.adtech.adms.order.OrderService;
import com.adtech.adms.order.capitek.Constants.ErrorMessage;
import com.adtech.core.exception.ServiceException;

public class OrderProcesser {
	private static final String OPEN_MESSAGE_REQUEST = "openMessageRequest";
	private DeviceService deviceService;
	private OrderService orderService;
	private static Logger log=Logger.getLogger(OrderProcesser.class);
	public OMElement open(OMElement input) {
		String code = Constants.ErrorCode.CODE_0000;
		String msg = Constants.ErrorMessage.CODE_0000;
		OpenMessage message = SOAPUtil.xmlToBean(((OMNode)input.getParent()).getParent().toString(),
				OPEN_MESSAGE_REQUEST, OpenMessage.class);

		GuiOrder order = new GuiOrder();
		order.setTopoSid(Long.parseLong(message.getService_code()));
		order.setImportFrom("NBI");
		order.setContent(input.toString());
		order.setSrcKey(message.getSequence());
		order.setOrderType("1");
		order.setSubOrderType(OPEN_MESSAGE_REQUEST);
		order.setResultStatus(ErrorMessage.CODE_0000);
		order.setImportDate(new Timestamp(System.currentTimeMillis()));
		order.setStatus("ok");
		GuiDevice device = new GuiDevice();

		device.setDevMac(message.getDevice_id());
		device.setMdn(message.getMdn());
		device.setTopoSid(Long.parseLong(message.getService_code()));
		device.setImsi(message.getUsername());
		device.setSubscriberAddress(message.getSubscriber_address());
		try {
			deviceService.add(device);
		} catch (Exception e) {
			log.error("save device error",e);
			code = Constants.ErrorCode.CODE_9999;
			msg = Constants.ErrorMessage.CODE_9999;
			order.setResultStatus(ErrorMessage.CODE_9999);
			return new ResponseMessage(code, msg).getResponse();
		}

		try {
			orderService.save(order);
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		return new ResponseMessage(code, msg).getResponse();
	}



	public OMElement suspend(OMElement input) {
		return new ResponseMessage(Constants.ErrorCode.CODE_0000,
				Constants.ErrorMessage.CODE_0000).getResponse();
	}

	public OMElement restore(OMElement input) {
		return new ResponseMessage(Constants.ErrorCode.CODE_0000,
				Constants.ErrorMessage.CODE_0000).getResponse();
	}

	public OMElement delete(OMElement input) {
		return new ResponseMessage(Constants.ErrorCode.CODE_0000,
				Constants.ErrorMessage.CODE_0000).getResponse();
	}

	public OMElement replaceUSIM(OMElement input) {
		return new ResponseMessage(Constants.ErrorCode.CODE_0000,
				Constants.ErrorMessage.CODE_0000).getResponse();
	}

	public OMElement updateCUG(OMElement input) {
		return new ResponseMessage(Constants.ErrorCode.CODE_0000,
				Constants.ErrorMessage.CODE_0000).getResponse();
	}
	public void setDeviceService(DeviceService deviceService) {
		this.deviceService = deviceService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
}
